/**
 *  Copyright 01.08.2012 oliver.keusemann@googlemail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package de.xelerity.geolessi.srv.mixed;

import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import de.xelerity.geolessi.types.poi.Abfahrt;

//12:23
public class AbfahrtszeitComperator implements Comparator<Abfahrt> {
	private long currentTime;

	public AbfahrtszeitComperator(long pCurrentTime) {
		currentTime = pCurrentTime;
	}

	public boolean isFuture(long pTimeinMillis) {
		GregorianCalendar lNow = new GregorianCalendar();
		GregorianCalendar X = new GregorianCalendar();

		X.setTime(new Date(pTimeinMillis));
		X.set(Calendar.SECOND, 00);

		lNow.setTime(new Date(currentTime));
		lNow.set(Calendar.SECOND, 00);

		if (X.get(Calendar.YEAR) > lNow.get(Calendar.YEAR)) {
			return true;
		} else if (X.get(Calendar.YEAR) < lNow.get(Calendar.YEAR)) {
			return false;
		} else if ((X.get(Calendar.MONTH) + 1) > (lNow.get(Calendar.MONTH) + 1)) {
			return true;
		} else if ((X.get(Calendar.MONTH) + 1) < (lNow.get(Calendar.MONTH) + 1)) {
			return false;
		} else if (X.get(Calendar.DATE) > lNow.get(Calendar.DATE)) {
			return true;
		} else if (X.get(Calendar.DATE) < lNow.get(Calendar.DATE)) {
			return false;
		} else if (X.get(Calendar.HOUR_OF_DAY) > lNow.get(Calendar.HOUR_OF_DAY)) {
			return true;
		} else if (X.get(Calendar.HOUR_OF_DAY) < lNow.get(Calendar.HOUR_OF_DAY)) {
			return false;
		} else if (X.get(Calendar.MINUTE) > lNow.get(Calendar.MINUTE)) {
			return true;
		} else if (X.get(Calendar.MINUTE) < lNow.get(Calendar.MINUTE)) {
			return false;
		} else if (X.get(Calendar.SECOND) > lNow.get(Calendar.SECOND)) {
			return true;
		} else if (X.get(Calendar.SECOND) < lNow.get(Calendar.SECOND)) {
			return false;
		}

		return true;
	}

	public boolean isFuture(String pDate) {

		GregorianCalendar lNow = new GregorianCalendar();
		GregorianCalendar X = new GregorianCalendar();

		String[] split = pDate.split(":");

		X.setTime(new Date(currentTime));
		X.set(Calendar.HOUR_OF_DAY, Integer.parseInt(split[0]));
		X.set(Calendar.MINUTE, Integer.parseInt(split[1]));
		X.set(Calendar.SECOND, 00);

		return isFuture(X.getTimeInMillis());
	}

	public int compare(Abfahrt A, Abfahrt B) {
		try {
			String _A, _B;
			if (isFuture(A.getAbfahrtzeitRealTime())) {
				_A = "A" + A.getAbfahrtzeitRealTime();
			} else {
				_A = "Z" + A.getAbfahrtzeitRealTime();
			}
			if (isFuture(B.getAbfahrtzeitRealTime())) {
				_B = "A" + B.getAbfahrtzeitRealTime();
			} else {
				_B = "Z" + B.getAbfahrtzeitRealTime();
			}
			return _A.toLowerCase().compareTo(_B.toLowerCase());
		} catch (Throwable e) {
			return 0;
		}
	}
}